package com.example.gateway.controller;

import com.alibaba.fastjson.JSON;
import com.example.gateway.request.UriReq;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class registerController {
    @Resource
    RedisTemplate redisTemplate;

    /**
     * postman测试结果在static
     * @param uri
     * @return
     */
    @PostMapping("/register")
    public String register(@RequestBody UriReq uri){
        List<UriReq> redisLpush = redisTemplate.opsForList().range("redisLpush", 0, -1);

        int size = redisLpush.stream().filter(item -> item.equals(uri)).collect(Collectors.toList()).size();

        if (size != 0) {
            //            /static/1.png
            return "服务已经存在，无需重复注册";
        }else {
            //            /static/0.png
            redisTemplate.opsForList().leftPush("redisLpush", uri);
            return "服务已进行注册";
        }
    }
}
